package com.brillilab.starter.controller.aio.method;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.method.MethodTypePowerList;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.method.MethodTypePowerLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/method/power")
public class MethodTypePowerController {

    @Resource
    private UsersLogic usersLogic;

    @Autowired
    private MethodTypePowerLogic methodTypePowerLogic;


    /**
     * 分级方法类型列表
     * @param token
     * @return
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public ResponseVo memberPowerList(@RequestHeader(StarterConstant.TOKEN)String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        MethodTypePowerList list=methodTypePowerLogic.getMemberMethodPowerList(userInfo.getLabMemberId());
        return ResponseVo.success(list);
    }

    /**
     * 修改分级方法类型列表
     * @param jsonObject
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public ResponseVo editMemberPower(@RequestBody JSONObject jsonObject,@RequestHeader(StarterConstant.TOKEN)String token){
        List<Long> powers=jsonObject.getJSONArray("powers").toJavaList(Long.class);
        Assert.isTrue(powers!=null && StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        boolean bool=methodTypePowerLogic.editMemberPower(powers,userInfo.getLabMemberId());
        if(bool){
            return ResponseVo.success();
        }else{
            return ResponseVo.failure();
        }
    }

}
